package vip.xiaonuo.hontu.issued.impl;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.iservice.IVisitorDeviceService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.modular.memberdevice.constant.MemberDeviceConstant;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.mapper.VisitorMapper;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.facedoor.rule.HTRulesEngine;
import vip.xiaonuo.facedoor.rule.RuleConstant;

@Service(value = RuleConstant.HT + RuleConstant.VISITOR_DEVICE)
public class HTVisitorDeviceServiceImpl implements IVisitorDeviceService {

    @Resource
    private VisitorMapper visitorMapper;

    @Resource
    private VisitorDeviceMapper visitorDeviceMapper;

    @Resource
    private DeviceBaseService deviceBaseService;

    private void baseCheck(VisitorDevice visitorDevice) {
        if (visitorDevice == null) {
            throw new ServiceException(500, "找不到关联信息！");
        }
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD) || visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE) || visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE)) {
            throw new ServiceException(500, "非正常状态 不允许操作！");
        }
    }


    @Override
    public void delete(Long id) throws Exception {
        VisitorDevice visitorDevice = visitorDeviceMapper.selectById(id);
        baseCheck(visitorDevice);
        DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
        Visitor visitor = visitorMapper.selectById(visitorDevice.getVisitorId());
        // 新增失败 直接删除人员
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            visitorDeviceMapper.deleteById(id);
        } else {
            HTRulesEngine.visitorDealWithService.delete(visitorDevice, visitor, null, deviceBase);

        }
    }

    @Override
    public void sync(Long id) throws Exception {
        VisitorDevice visitorDevice = this.visitorDeviceMapper.selectById(id);
        baseCheck(visitorDevice);
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.SYNC_SUCCESS)) {
            throw new ServiceException(500, "未发生错误 不允许同步！");
        }
        DeviceBase deviceBase = deviceBaseService.getById(visitorDevice.getDeviceId());
        Visitor visitor = visitorMapper.selectById(visitorDevice.getVisitorId());
        // 恢复人员状态
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.ADD_FAIL)) {
            HTRulesEngine.visitorDealWithService.add(visitorDevice, visitor, null, deviceBase);
        }
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.UPDATE_FAIL)) {
            HTRulesEngine.visitorDealWithService.update(visitorDevice, visitor, null, deviceBase);
        }
        if (visitorDevice.getStatus().equals(MemberDeviceConstant.DELETE_FAIL)) {
            HTRulesEngine.visitorDealWithService.delete(visitorDevice, visitor, null, deviceBase);
        }
        visitorDeviceMapper.updateById(visitorDevice);
    }
}
